// 
//  Author:
//    Andy Selvig ajselvig@gmail.com
// 
//  Copyright (c) 2010, Andy Selvig
// 
//  All rights reserved.
// 
//  Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
// 
//     * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in
//       the documentation and/or other materials provided with the distribution.
//     * Neither the name of the [ORGANIZATION] nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
// 
//  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
//  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
//  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
//  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
//  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
//  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
//  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
//  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
//  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
//  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
//  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// 

using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;

namespace Shake
{
	/// <summary>
	/// Base class for classes that contain tasks.
	/// </summary>
	public abstract class TaskGroup
    {
        /// <summary>
        /// Finds all of the tasks in this class.
        /// </summary>
        public TaskGroup()
        {
        	// get all the tasks
        	Tasks = new List<TaskAttribute>();
        	var methods = GetType().GetMethods();
        	foreach (var method in methods)
            {
        		var attrs = method.GetCustomAttributes(typeof(TaskAttribute), true);
        		if (attrs.Length > 0)
                {
        			var task = attrs[0] as TaskAttribute;
        			task.Name = method.Name;
					task.MethodInfo = method;
					task.Group = this;
        			Tasks.Add(task);
        		}
        	}

            // get the namespace, if there is one
        	var myAttrs = GetType().GetCustomAttributes(typeof(TaskNamespaceAttribute), true);
        	if (myAttrs.Length > 0)
            {
        		var nsAttr = myAttrs[0] as TaskNamespaceAttribute;
                Namespace = nsAttr.Namespace;
            }
        }		
		
		/// <summary>
		/// The namespace for all tasks in this group.
		/// </summary>
		public string Namespace { get; set; }
		
		/// <summary>
		/// Use this to write output from a task.
		/// </summary>
		public TextWriter Output { get; set; }
				
		/// <summary>
		/// The tasks in this group.
		/// </summary>
		public List<TaskAttribute> Tasks { get; private set; }
		
		/// <summary>
		/// Gets the task with the given, unqualified name.
		/// </summary>
		public TaskAttribute GetTask(string name)
		{
			foreach (var task in Tasks) {
				if (task.Name == name)
					return task;
			}
			return null;
		}
		
	}
}

